package com.bkgy.modular.tbdsjplancheckpdiC622.service;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.alibaba.fastjson.JSONObject;
import com.bkgy.excelhandle.PlanExcelHandlerImpl;
import com.bkgy.modular.datacollect_bx.bean.JDataSave622;
import com.bkgy.modular.qms.entity.QmsCurveDataMongo;
import com.bkgy.modular.tbdsjplancheckpdiC622.dao.PlanCheckPdi622Dao;
import com.bkgy.modular.tbdsjplancheckpdiC622.entity.ExportPlanCheckPdi622;
import com.bkgy.modular.tbdsjplancheckpdiC622.entity.PlanCheckPdi622;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.ExcelUtilsPOI;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PlanCheckPdi622Service {

    @Resource
    private PlanCheckPdi622Dao planCheckPdi622Dao;
    @Resource
    private MongoTemplate mongoTemplate;

    public ResultData findDataByPage(JSONObject json) {
        try {
            /*Integer pageIndex = json.getInteger("pageIndex");
            Integer pageSize = json.getInteger("pageSize");
            if (pageIndex == null || pageSize == null) {
                return ResultData.ResultDataFaultSelf("分页参数没有传", null);
            }*/
            //查询条件
            String hot_coil_id = null;
            String plan_no = null;
            String dbegin = null;
            String dend = null;
            Double trim_warn = null;
            JSONObject condition = json.getJSONObject("condition");
            if (!condition.isEmpty()) {
                if (StringUtils.hasText(condition.getString("hot_coil_id"))) {
                    hot_coil_id = condition.getString("hot_coil_id");
                }
                if (StringUtils.hasText(condition.getString("plan_no"))) {
                    plan_no = condition.getString("plan_no");
                }
                if (StringUtils.hasText(condition.getString("dbegin"))) {
                    dbegin = condition.getString("dbegin");
                }
                if (StringUtils.hasText(condition.getString("dend"))) {
                    dend = condition.getString("dend");
                }
                if (StringUtils.hasText(condition.getString("trim_warn"))) {
                    trim_warn = condition.getDouble("trim_warn");
                }
            }
            // (pageIndex - 1) * pageSize, pageIndex * pageSize,
            List<PlanCheckPdi622> list = planCheckPdi622Dao.findDataByPage(dbegin, dend, hot_coil_id, plan_no,trim_warn);
            Integer count = planCheckPdi622Dao.findDataCount(dbegin, dend, hot_coil_id, plan_no,trim_warn);
            return ResultData.ResultDataSuccess(list, count);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
        }
    }

    /**
     * 列表导出
     * @param jsonObject
     * @param response
     */
    public void export(JSONObject jsonObject, HttpServletResponse response) {
        String fileNames = jsonObject.getString("fileName");
        //查询条件
        String hot_coil_id = null;
        String plan_no = null;
        String dbegin = null;
        String dend = null;
        Double trim_warn = null;
        JSONObject condition = jsonObject.getJSONObject("condition");
        if (!condition.isEmpty()) {
            if (StringUtils.hasText(condition.getString("hot_coil_id"))) {
                hot_coil_id = condition.getString("hot_coil_id");
            }
            if (StringUtils.hasText(condition.getString("plan_no"))) {
                plan_no = condition.getString("plan_no");
            }
            if (StringUtils.hasText(condition.getString("dbegin"))) {
                dbegin = condition.getString("dbegin");
            }
            if (StringUtils.hasText(condition.getString("dend"))) {
                dend = condition.getString("dend");
            }
            if (StringUtils.hasText(condition.getString("trim_warn"))) {
                trim_warn = condition.getDouble("trim_warn");
            }
        }
        // 创建表格数据集合
        List<PlanCheckPdi622> listExcel = new ArrayList<>();
        // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
        List<PlanCheckPdi622> data = planCheckPdi622Dao.findDataByPage(dbegin, dend, hot_coil_id, plan_no,trim_warn);
        for (PlanCheckPdi622 item : data) {
            listExcel.add(item);
        }
        // 判断service查询出的数据集合data是否为空
        if (CollectionUtils.isEmpty(data)) {
            return;
        }
        List<ExportPlanCheckPdi622> list = new ArrayList<>();
        for (PlanCheckPdi622 item : listExcel) {
            ExportPlanCheckPdi622 exportPlanCheckPdi622 = new ExportPlanCheckPdi622();
            BeanUtils.copyProperties(item,exportPlanCheckPdi622);
            list.add(exportPlanCheckPdi622);
        }
        try {
            Map<String,Object> data1 = new HashMap();
            data1.put("data",list);
            File file = ResourceUtils.getFile("classpath:exceltemplate\\2130生产计划审核.xlsx");
            TemplateExportParams templateExportParams = new TemplateExportParams(file.getPath(),false);
            templateExportParams.setDictHandler(new PlanExcelHandlerImpl());
            Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, data1);
            ExcelUtilsPOI.exportExcel(fileNames,response,workbook);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public ResultData findLineDataByVolumeNo(JSONObject json) {
        String volumeNo = json.getString("volumeNo");
        if (!StringUtils.hasText(volumeNo)) {
            return ResultData.ResultDataFaultSelf(null, "传入卷号为空");
        }

        Map<String, Map<String, Object>> map = new HashMap<>();
        Query query1 = new Query(Criteria.where("volumeNo").is(volumeNo));
        QmsCurveDataMongo qmsCurveDataMongo = mongoTemplate.findOne(query1, QmsCurveDataMongo.class);


        Query query = new Query(Criteria.where("extid").is(volumeNo));
        JDataSave622 jDataSave622 = mongoTemplate.findOne(query, JDataSave622.class);
        // qms_curve_data_mongo集合
        Map<String, Object> mapValue = new HashMap<>();
        if(!ObjectUtils.isEmpty(qmsCurveDataMongo)){
            List<Double> list1 = qmsCurveDataMongo.getOriginBolg().get("hsm_fdh_thk_cold");
            List<Double> list2 = qmsCurveDataMongo.getOriginBolg().get("hsm_fdw_width_cold");
            List<Double> list3 = qmsCurveDataMongo.getOriginBolg().get("hsm_crown_c40");
            List<Double> list4 = qmsCurveDataMongo.getOriginBolg().get("hsm_flatness_wedge40");
            List<Double> list5 = qmsCurveDataMongo.getOriginBolg().get("hsm_fdt_fdt");
            List<Double> list6 = qmsCurveDataMongo.getOriginBolg().get("hsm_ctc_ct_temp");
            List<Double> list7 = qmsCurveDataMongo.getOriginBolg().get("hsm_flatness_symmetric_flatness");
            List<Double> list8 = qmsCurveDataMongo.getOriginBolg().get("hsm_flatness_asymmetric_flatness");

            mapValue.put("hsm_fdh_thk_cold", list1);
            mapValue.put("hsm_fdw_width_cold", list2);
            mapValue.put("hsm_crown_c40", list3);
            mapValue.put("hsm_flatness_wedge40", list4);
            mapValue.put("hsm_fdt_fdt", list5);
            mapValue.put("hsm_ctc_ct_temp", list6);
            mapValue.put("hsm_flatness_symmetric_flatness", list7);
            mapValue.put("hsm_flatness_asymmetric_flatness", list8);
        }

        // datacollect622集合
        Map<String,Object> jDataSaveList = new HashMap<>();
        if (!ObjectUtils.isEmpty(jDataSave622)) {
            List<Double> list1 = jDataSave622.getRow().get("c622_tcm_exs5_gaugedev1_thk");
            List<Double> list2 = jDataSave622.getRow().get("c622_tcm_exs4_shape_width_act");
            List<Double> list3 = jDataSave622.getRow().get("c622_s5_speed");
            List<Double> list4 = jDataSave622.getRow().get("c622_tcm_shape_channel1");
            List<Double> list5 = jDataSave622.getRow().get("c622_tcm_shape_channel2");
            List<Double> list6 = jDataSave622.getRow().get("c622_tcm_shape_channel3");
            List<Double> list7 = jDataSave622.getRow().get("c622_tcm_shape_channel4");
            List<Double> list8 = jDataSave622.getRow().get("c622_tcm_shape_channel5");
            List<Double> list9 = jDataSave622.getRow().get("c622_tcm_shape_channel6");
            List<Double> list10 = jDataSave622.getRow().get("c622_tcm_shape_channel7");
            List<Double> list11 = jDataSave622.getRow().get("c622_tcm_shape_channel8");
            List<Double> list12 = jDataSave622.getRow().get("c622_tcm_shape_channel9");
            List<Double> list13 = jDataSave622.getRow().get("c622_tcm_shape_channel10");
            List<Double> list14 = jDataSave622.getRow().get("c622_tcm_shape_channel11");
            List<Double> list15 = jDataSave622.getRow().get("c622_tcm_shape_channel12");
            List<Double> list16 = jDataSave622.getRow().get("c622_tcm_shape_channel13");
            List<Double> list17 = jDataSave622.getRow().get("c622_tcm_shape_channel14");
            List<Double> list18 = jDataSave622.getRow().get("c622_tcm_shape_channel15");
            List<Double> list19 = jDataSave622.getRow().get("c622_tcm_shape_channel16");
            List<Double> list20 = jDataSave622.getRow().get("c622_tcm_shape_channel17");
            List<Double> list21 = jDataSave622.getRow().get("c622_tcm_shape_channel18");
            List<Double> list22 = jDataSave622.getRow().get("c622_tcm_shape_channel19");
            List<Double> list23 = jDataSave622.getRow().get("c622_tcm_shape_channel20");
            List<Double> list24 = jDataSave622.getRow().get("c622_tcm_shape_channel21");
            List<Double> list25 = jDataSave622.getRow().get("c622_tcm_shape_channel22");
            List<Double> list26 = jDataSave622.getRow().get("c622_tcm_shape_channel23");
            List<Double> list27 = jDataSave622.getRow().get("c622_tcm_shape_channel24");
            List<Double> list28 = jDataSave622.getRow().get("c622_tcm_shape_channel25");
            List<Double> list29 = jDataSave622.getRow().get("c622_tcm_shape_channel26");
            List<Double> list30 = jDataSave622.getRow().get("c622_tcm_shape_channel27");
            List<Double> list31 = jDataSave622.getRow().get("c622_tcm_shape_channel28");
            List<Double> list32 = jDataSave622.getRow().get("c622_tcm_shape_channel29");
            List<Double> list33 = jDataSave622.getRow().get("c622_tcm_shape_channel30");
            List<Double> list34 = jDataSave622.getRow().get("c622_tcm_shape_channel31");
            List<Double> list35 = jDataSave622.getRow().get("c622_tcm_shape_channel32");
            List<Double> list36 = jDataSave622.getRow().get("c622_tcm_shape_channel33");
            List<Double> list37 = jDataSave622.getRow().get("c622_tcm_shape_channel34");
            List<Double> list38 = jDataSave622.getRow().get("c622_tcm_shape_channel35");
            List<Double> list39 = jDataSave622.getRow().get("c622_tcm_shape_channel36");
            List<Double> list40 = jDataSave622.getRow().get("c622_tcm_shape_channel37");
            List<Double> list41 = jDataSave622.getRow().get("c622_tcm_shape_channel38");
            List<Double> list42 = jDataSave622.getRow().get("c622_tcm_shape_channel39");
            List<Double> list43 = jDataSave622.getRow().get("c622_tcm_shape_channel40");
            jDataSaveList.put("c622_tcm_exs5_gaugedev1_thk",list1);
            jDataSaveList.put("c622_tcm_exs4_shape_width_act",list2);
            jDataSaveList.put("c622_s5_speed",list3);
            jDataSaveList.put("c622_tcm_shape_channel1",list4);
            jDataSaveList.put("c622_tcm_shape_channel2",list5);
            jDataSaveList.put("c622_tcm_shape_channel3",list6);
            jDataSaveList.put("c622_tcm_shape_channel4",list7);
            jDataSaveList.put("c622_tcm_shape_channel5",list8);
            jDataSaveList.put("c622_tcm_shape_channel6",list9);
            jDataSaveList.put("c622_tcm_shape_channel7",list10);
            jDataSaveList.put("c622_tcm_shape_channel8",list11);
            jDataSaveList.put("c622_tcm_shape_channel9",list12);
            jDataSaveList.put("c622_tcm_shape_channel10",list13);
            jDataSaveList.put("c622_tcm_shape_channel11",list14);
            jDataSaveList.put("c622_tcm_shape_channel12",list15);
            jDataSaveList.put("c622_tcm_shape_channel13",list16);
            jDataSaveList.put("c622_tcm_shape_channel14",list17);
            jDataSaveList.put("c622_tcm_shape_channel15",list18);
            jDataSaveList.put("c622_tcm_shape_channel16",list19);
            jDataSaveList.put("c622_tcm_shape_channel17",list20);
            jDataSaveList.put("c622_tcm_shape_channel18",list21);
            jDataSaveList.put("c622_tcm_shape_channel19",list22);
            jDataSaveList.put("c622_tcm_shape_channel20",list23);
            jDataSaveList.put("c622_tcm_shape_channel21",list24);
            jDataSaveList.put("c622_tcm_shape_channel22",list25);
            jDataSaveList.put("c622_tcm_shape_channel23",list26);
            jDataSaveList.put("c622_tcm_shape_channel24",list27);
            jDataSaveList.put("c622_tcm_shape_channel25",list28);
            jDataSaveList.put("c622_tcm_shape_channel26",list29);
            jDataSaveList.put("c622_tcm_shape_channel27",list30);
            jDataSaveList.put("c622_tcm_shape_channel28",list31);
            jDataSaveList.put("c622_tcm_shape_channel29",list32);
            jDataSaveList.put("c622_tcm_shape_channel30",list33);
            jDataSaveList.put("c622_tcm_shape_channel31",list34);
            jDataSaveList.put("c622_tcm_shape_channel32",list35);
            jDataSaveList.put("c622_tcm_shape_channel33",list36);
            jDataSaveList.put("c622_tcm_shape_channel34",list37);
            jDataSaveList.put("c622_tcm_shape_channel35",list38);
            jDataSaveList.put("c622_tcm_shape_channel36",list39);
            jDataSaveList.put("c622_tcm_shape_channel37",list40);
            jDataSaveList.put("c622_tcm_shape_channel38",list41);
            jDataSaveList.put("c622_tcm_shape_channel39",list42);
            jDataSaveList.put("c622_tcm_shape_channel40",list43);
        }
        map.put(volumeNo, mapValue);
        map.put(volumeNo,jDataSaveList);
        if (!CollectionUtils.isEmpty(map)) {
            return ResultData.ResultDataSuccess(map);
        }
        return ResultData.ResultDataFaultSelf(null, "查询数据为空");
    }
}
